package panel.viewSpots
{
	import assists.HtmlText;
	import assists.SizeManager;
	import assists.TextFieldUtils;
	import assists.log.Log;
	import assists.resource.ImageResource;
	import assists.resource.ResourceLoader;
	
	import flash.display.Bitmap;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import panel.stars.StarGroup;
	import panel.viewSpots.navigation.ScrollTxtHor;
	
	import struct.Scenic;
	
	public class ViewSpotPic extends Sprite
	{
		private var _loading:Bitmap;
		private var _showPic:ImageResource;
		protected var _nameTxt:TextField;
		protected var _starGroup:StarGroup;
		private var _loader:ResourceLoader;
		private var _stage:Stage;
		private var _posId:uint;
		private var _blackRect:Shape;
		private var _openDetail:Function;
		private var _isClick:Boolean;
		private var _data:Scenic;
		private var _scrollTxt:ScrollTxtHor;
		public function ViewSpotPic(stage:Stage,loader:ResourceLoader)
		{
			_stage = stage;
			_loader = loader;
			super();
			this.cacheAsBitmap = true;
		}
		
		public function init(data:Scenic):void
		{
			_data = data;
			_loading = new _loader.uiManager.loadingPng();
			this.addChild(_loading);
			_loading.scaleY = _loading.scaleX = (SizeManager.fromeNumToscale(2,_loading.width));
			this.y = int(_posId / 2) * (this.height + this.height /4);
			this.x = (_posId % 2) * this.width + this.width * 0.2 * ((_posId % 2) + 1);
		
			_scrollTxt = new ScrollTxtHor();
			_scrollTxt.showWid = _loading.width;
			_scrollTxt.label(_data.name,0xffffff);
			_scrollTxt.mouseEnabled = false;
			
			_starGroup = new StarGroup();
			_starGroup.mouseEnabled = false;
			_starGroup.starLevel = data.star;
			_starGroup.scaleX = _starGroup.scaleY = 0.5;
			_blackRect = new Shape();
			this.addEventListener(MouseEvent.MOUSE_UP,clickHandler);
			this.addEventListener(MouseEvent.MOUSE_DOWN,downhandler);
			_showPic = new ImageResource(_loader,data.url,showPic);
			this.addChild(_showPic);
		}
		
		protected function downhandler(event:MouseEvent):void
		{
			_isClick = true;
			trace("down item");
		}
		
		protected function clickHandler(event:MouseEvent):void
		{
			trace("click item up");
			if(!_isClick) return;
			_isClick = false;
			_openDetail(_data);
		}
		
		public function set posId(posId:uint):void
		{
			_posId = posId;
		}
		
		private function showPic():void
		{
			_showPic.addChild(_blackRect);
			_blackRect.graphics.beginFill(0,0.6);
			_blackRect.graphics.drawRect(0,_showPic.height * .8,_showPic.width,_showPic.height * .2)
			_blackRect.graphics.endFill();
			
			_showPic.width = _loading.width
			_showPic.height = _showPic.width;
			this.addChild(_scrollTxt);
			this.addChild(_starGroup);
			this.removeChild(_loading);
			resize();
		}
		
		private function resize():void
		{
			_starGroup.y = _showPic.height - _starGroup.height;
			_starGroup.x = (_showPic.width - _starGroup.width)/2;
			
			_scrollTxt.x = (_showPic.width - _scrollTxt.width)/2;
			_scrollTxt.y = _showPic.height - _scrollTxt.height - _starGroup.height * 0.9;
		}
		
		public function set onOpenDetail(value:Function):void
		{
			_openDetail = value;
		}
		
		public function clear():void
		{
			this.removeEventListener(MouseEvent.CLICK,clickHandler);
		}
		
	}
}